﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Modbus.Device;
using YJYpublicClass;

namespace WindowsFormsApp2.ModbusRtuSet
{
    public class NModbus4Rtu
    {
        public Boolean IsConnect = false;
        public string rtuSavePath = "ModbusRtu.xml";

        private static object lockobj = new object();
        public string Mes
        {
            get;
            set;
        }
        public ModbusRtuParams rtuConfig { get; set; } = new ModbusRtuParams();


        private SerialPort serialPort = new SerialPort();


        private IModbusMaster master;


        public Boolean Connect()
        {
            try
            {
                if (serialPort.IsOpen)
                {
                    return true;
                }

                //设置串口
                serialPort.PortName = rtuConfig.Com;
                serialPort.BaudRate = rtuConfig.BaudRate;
                serialPort.Parity = rtuConfig._Parity;
                serialPort.DataBits = rtuConfig.DataBits;
                serialPort.StopBits = rtuConfig._StopBits;
                serialPort.Open();

                master = ModbusSerialMaster.CreateRtu(serialPort);
                IsConnect = serialPort.IsOpen;

                return IsConnect;
            }
            catch (Exception ex)
            {

                MessageBox.Show("rtu串口打开失败");
                return false;
            }
        }


        public Boolean Init()
        {
            LoadConfig();
            if (Connect())
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public void Close()
        {
            serialPort.Close();
        }


        public bool isConnect()
        {
            return IsConnect;
        }

        public void ReadShort(string address, out ushort value)
        {
            ushort[] result = new ushort[1];
            try
            {

                if (IsConnect)
                {
                    result = master.ReadHoldingRegisters((byte)1, Convert.ToUInt16(address), 1);
                    value = result[0];

                }
                else
                {
                    value = result[0];
                }
            }
            catch (Exception ex)
            {
                value = result[0];
                Mes = ex.ToString();
            }

        }

        public Boolean WriteShort(string address, ushort value)
        {
            try
            {
                if (IsConnect)
                {
                    master.WriteSingleRegister((byte)1, Convert.ToUInt16(address), value);

                   return true;

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public ResultFunc Saveconfig()
        {
            try
            {
                if (XmlHelper.SaveXML(rtuSavePath, rtuConfig) == "")
                {
                    return new ResultFunc(Funstate.OK, "");
                }
                else
                {
                    return new ResultFunc(Funstate.异常, "保存失败");
                }
            }
            catch (Exception ex)
            {

                return new ResultFunc(Funstate.异常, ex.ToString());
            }

        }

        public void LoadConfig()
        {
            if (!File.Exists(rtuSavePath))
            {
                rtuConfig = new ModbusRtuParams();
            }
            else
            {
                rtuConfig = XmlHelper.LoadXML<ModbusRtuParams>(rtuSavePath);
            }
        }

        public void setConfig()
        {
            Frm_Rtu tempform = new Frm_Rtu(this);
            tempform.Show();
        }

    }
}
